package com.boot.base.serviceImpl;

import com.boot.base.service.BaseService;
import com.boot.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.boot.base.dao.BaseDao;

import java.util.List;
import java.util.Map;

/**
 * 实现类
 *
 * @param <T>
 * @author 【zhouJie】
 */
public class BaseServiceImpl<T> implements BaseService<T> {

    private static final Logger logger = LoggerFactory.getLogger(BaseServiceImpl.class);

    public BaseServiceImpl() {
    }

    @Autowired
    private BaseDao<T> baseDao;

    @Transactional
    @Override
    public int save(T t) {
        try {
            return baseDao.save(t);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BusinessException("添加失败");
        }
    }

    @Transactional
    @Override
    public int delete(Integer id) {
        try {
            return baseDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BusinessException("删除失败");
        }
    }

    @Transactional
    @Override
    public int batchDelete(Integer[] ids) {
        try {
            return baseDao.batchDelete(ids);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BusinessException("批量删除失败");
        }
    }

    @Transactional
    @Override
    public int update(T t) {
        try {
            return baseDao.update(t);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BusinessException("修改失败");
        }
    }

    @Override
    public T getById(Integer id) {
        try {
            return baseDao.queryById(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BusinessException("查询失败");
        }
    }

    @Override
    public List<T> getPageList(Map<String, Object> map) {
        try {
            return baseDao.queryPageList(map);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BusinessException("查询失败");
        }
    }

    @Override
    public List<T> getAll() {
        try {
            return baseDao.queryAll();
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BusinessException("查询失败");
        }
    }

}
